class DSU:
    def __init__(self, n):
        self.par = [i for i in range(n)]

    def find(self, n):
        par = self.par[n]
        if par == n:
            return par
        par = self.find(par)
        self.par[n] = par
        return par

    def union(self, x, y):
        xp = self.find(x)
        yp = self.find(y)
        self.par[xp] = yp


class Solution:
    def makeConnected(self, n: int, connections: list) -> int:
        if len(connections) < n - 1:
            return -1
        dsu = DSU(n)
        for conn in connections:
            dsu.union(conn[0], conn[1])

        clusters = set()
        for i in range(n):
            clusters.add(dsu.find(i))

        return len(clusters) - 1
